﻿using System;
using System.Xml.Serialization;
using System.IO;

namespace com.diagnosis.tool.xml
{
    public class DiagnosisToolXmlMngr
    {        
        private static volatile DiagnosisToolXmlMngr sMngr;

        private static readonly Object sSync = new Object();

        private static DiagnosisToolData sDiagnosisToolData;

        private static String sFilePath;
        
        private DiagnosisToolXmlMngr(String pFilePath)
        {
            sFilePath = pFilePath;
            
            sDiagnosisToolData = Deserialize();
        }

        public static DiagnosisToolXmlMngr GetInstance(String pFilePath)
        {
            if (null == sMngr)
            {
                lock (sSync)
                {
                    if (null == sMngr)
                    {
                        sMngr = new DiagnosisToolXmlMngr(pFilePath);
                    }
                }
            }
            return sMngr;
        }

        public DiagnosisToolData DiagnosisToolData 
        {
            get
            {
                return sDiagnosisToolData;
            }
        }

        static public DiagnosisToolData Deserialize()
        {
            var serializer = new XmlSerializer(typeof(DiagnosisToolData));

            var reader = new StreamReader(sFilePath);
            var dtd = (DiagnosisToolData)serializer.Deserialize(reader);
            reader.Close();

            return dtd;
        }

        static public void SaveToXML(DiagnosisToolData pDtool, String pFilePath)
        {
            var serializer = new XmlSerializer(typeof(DiagnosisToolData));
            TextWriter textWriter = new StreamWriter(pFilePath);
            serializer.Serialize(textWriter, pDtool);
            textWriter.Close();
        }
       
    }
}
